package org.example.shoppingmall.dao;

import org.example.shoppingmall.entity.User;
import org.example.shoppingmall.util.HikariUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    /**
     *添加用户
     * @param user
     * @return
     */
    public boolean insertUser(User user) {
        String sql = "INSERT INTO tb_user (username, password, email,role,phone,avatar,nickname,balance) VALUES (?, ?,?,?, ?,?,?,?)";
        try (Connection conn = HikariUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            ps.setInt(4,0);
            ps.setString(5,user.getPhone());
            ps.setString(6,user.getAvatar());
            ps.setString(7,user.getNickname());
            ps.setBigDecimal(8, user.getBalance());
            int rows = ps.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    public boolean isUsernameExists(String username){
        String sql = "SELECT COUNT(*) FROM tb_user WHERE username = ?";
        try(Connection conn = HikariUtil.getConn();
            PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setString(1,username);
            try(ResultSet rs = ps.executeQuery()){
                if(rs.next()){
                    int count = rs.getInt(1);
                    return count > 0;
                }
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据邮箱查找用户
     * @param email
     * @return
     */
    public boolean isEmailExists(String email) {
        String sql = "SELECT COUNT(*) FROM tb_user WHERE email = ?";
        try (Connection conn = HikariUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, email);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    int count = rs.getInt(1);
                    return count > 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据账号密码获取用户
     * @param username
     * @param password
     * @return
     */
    public User getUserByUsernameAndPassword(String username, String password) {
        String sql = "SELECT * FROM tb_user WHERE username = ? AND password = ?";
        try (Connection conn = HikariUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    return user;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据用户ID获取用户
     * @param userId
     * @return
     */
    public User getUserById(String userId) {
        String sql = "SELECT * FROM tb_user WHERE id = ?";
        try (Connection conn = HikariUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, Integer.parseInt(userId));
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    return user;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据用户ID修改个人信息
     * @param userId
     * @param email


     * @param nickname
     * @param phone
     * @return
     */
    public Boolean updateUserByID(String userId,String email, String nickname, String phone){
        String sql = "UPDATE tb_user SET email=?,nickname=?,phone=? WHERE id = ?";
        try (Connection conn = HikariUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1,email);
            ps.setString(2,nickname);
            ps.setString(3,phone);
            ps.setInt(4,Integer.parseInt(userId));
            int rows = ps.executeUpdate();
            return rows > 0;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 申请升级
     * @param userId
     * @return
     */
    public boolean upGrade(String userId){
        String sql = "UPDATE tb_user SET role = ? WHERE id = ?";
        int rows = 0;
        try(Connection conn = HikariUtil.getConn();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1,3);
            ps.setInt(2,Integer.parseInt(userId));
            rows = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rows > 0;
    }

    /**
     * 根据id获取头像
     * @param userId
     * @param avatar
     * @return
     */
    public boolean uploadAvatar(String userId,String avatar){
        String sql = "UPDATE tb_user SET avatar = ? WHERE id = ?";
        int rows = 0;
        try(Connection conn = HikariUtil.getConn();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1,avatar);
            ps.setInt(2,Integer.parseInt(userId));
            rows = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rows > 0;
    }
}
